package com.molyfun.parents.fragment.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.view.StrokeTextView;
import com.molyfun.parents.view.common.MfEditText;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class CommonInputDialogFragment extends DialogFragment implements View.OnClickListener{

    @InjectView(R.id.content)
    MfEditText mMfEditText;
    @InjectView(R.id.cancel)
    StrokeTextView mCancel;
    @InjectView(R.id.title)
    TextView mTitle;


    private static CommonInputDialogFragment newInstance(String title, String content, String hint) {
        CommonInputDialogFragment mStudentMenuDialogFragment = new CommonInputDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(Const.FLAG_TITLE, title);
        bundle.putSerializable(Const.FLAG_CONTENT, content);
        bundle.putSerializable(Const.FLAG_HINT, hint);
        mStudentMenuDialogFragment.setArguments(bundle);
        return mStudentMenuDialogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setStyle(DialogFragment.STYLE_NORMAL, R.style.BaseDialog);
    }

    @Override public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        dialog.setCanceledOnTouchOutside(false);
        if (null != dialog) {
            dialog.getWindow().setLayout(-1, -2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        window.setAttributes(lp);

        View view = inflater.inflate(R.layout.dialog_input_common, container, false);

        ButterKnife.inject(this, view);
        String content = (String)getArguments().get(Const.FLAG_CONTENT);
        mTitle.setText((String)getArguments().get(Const.FLAG_TITLE));

        String hint = (String)getArguments().get(Const.FLAG_HINT);
        if(!TextUtils.isEmpty(hint)){
            mMfEditText.setHint(hint);
        }

        mMfEditText.setText(content);
        mMfEditText.requestFocus();
        mCancel.setOnClickListener(this);
        view.findViewById(R.id.close).setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close:
                if(mOnCommonInputCallback != null){
                    mOnCommonInputCallback.onInputConfirm(mMfEditText.getText().toString());
                }
                break;
            case R.id.cancel:
                break;
            default:
                break;
        }
        getDialog().dismiss();
    }

    public static interface OnCommonInputCallback{
        public void onInputConfirm(String content);
    }

    private OnCommonInputCallback mOnCommonInputCallback;

    public static class CommonInputDialogBuilder {
        CommonInputDialogFragment mCommonDialogFragment;
        private String mTag;

        public CommonInputDialogBuilder(String content){
            mCommonDialogFragment = CommonInputDialogFragment.newInstance("提示", content, "");
            mTag = "common_input";
        }

        public CommonInputDialogBuilder(String title, String content){
            mCommonDialogFragment = CommonInputDialogFragment.newInstance(title, content, "");
            mTag = "common_input";
        }

        public CommonInputDialogBuilder(String title, String content, String hint){
            mCommonDialogFragment = CommonInputDialogFragment.newInstance(title, content, hint);
            mTag = "common_input";
        }


        public CommonInputDialogBuilder setOnCommonInputCallback(OnCommonInputCallback onCommonInputCallback){
            mCommonDialogFragment.mOnCommonInputCallback = onCommonInputCallback;
            return this;
        }

        public CommonInputDialogBuilder setTag(String tag){
            this.mTag = tag;
            return this;
        }

        public void build(FragmentTransaction fragmentTransaction){
//            mCommonDialogFragment.show(fragmentTransaction, mTag);
            fragmentTransaction.add(mCommonDialogFragment, mTag);
            fragmentTransaction.commitAllowingStateLoss();
        }

    }
}
